package com.portal.gui.marketmanger;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.portal.gui.marketman.EditCustomerScr;
import com.portal.logic.ConnectionManager;
import com.portal.logic.Customer;
import com.portal.logic.CustomerDAO;
import com.portal.logic.Feedback;
import com.portal.logic.FeedbackDAO;
import com.portal.logic.Product;
import com.portal.logic.ProductDAO;
import javax.swing.JTable;

public class MarketManagerPortal extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MarketManagerPortal frame = new MarketManagerPortal();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MarketManagerPortal() {
		setTitle("Market Manager Portal");
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JComboBox cmbProducts = new JComboBox();
		ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
		List<Product> products = productDAO.selectAll();
		Iterator<Product> iterator = products.iterator();
		while (iterator.hasNext()) {
			Product product = (Product) iterator.next();
			cmbProducts.addItem(product.getName());
		}
		cmbProducts.setBounds(110, 87, 88, 22);
		contentPane.add(cmbProducts);
		
		JButton btnGetFeedbackReport = new JButton("Get Feedback Report");
		btnGetFeedbackReport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO open other window
				String selectedProduct = (String)cmbProducts.getSelectedItem();
				GetFeedBackReportByProductScr getFeedBackReportByProductScr = new GetFeedBackReportByProductScr(selectedProduct);
				getFeedBackReportByProductScr.setVisible(true);
				
			/*	ProductDAO productDAO = new ProductDAO(ConnectionManager.getConnection());
				Product product = productDAO.selectByName(selectedProduct);
				
				FeedbackDAO feedbackDAO = new FeedbackDAO(ConnectionManager.getConnection()) ;
				List<Feedback> feedbacks = feedbackDAO.selectFeedbacksByProductId(product.getId());*/
				
			}
		});
		btnGetFeedbackReport.setBounds(214, 86, 182, 25);
		contentPane.add(btnGetFeedbackReport);
		
		JLabel lblSelectSale = new JLabel("Select Product");
		lblSelectSale.setBounds(12, 90, 86, 16);
		contentPane.add(lblSelectSale);
		
		JButton btnAddSale = new JButton("Add New Sale Screen");
		btnAddSale.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddSaleScr addSaleScr = new AddSaleScr();
				addSaleScr.setVisible(true);
			}
		});
		btnAddSale.setBounds(161, 13, 182, 25);
		contentPane.add(btnAddSale);
		
		final JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				Container frame = btnBack.getParent();
				do{
					frame = frame.getParent(); 
				}while (!(frame instanceof JFrame));                                      
				((JFrame) frame).dispose();
			}
		});
		btnBack.setBounds(12, 203, 97, 25);
		contentPane.add(btnBack);
		
		
		final CustomerDAO customerDAO = new CustomerDAO(ConnectionManager.getConnection()) ;
		List<Customer> customers = customerDAO.selectAll();
		
		final JComboBox cmbCustomers = new JComboBox();
		Iterator<Customer> iterator2 = customers.iterator();
		while (iterator2.hasNext()) {
			Customer customer = (Customer) iterator2.next();
			cmbCustomers.addItem(customer.getName());
		}
		cmbCustomers.setBounds(166, 157, 97, 22);
		contentPane.add(cmbCustomers);
		
		JLabel lblSelectCustomerTo = new JLabel("Select Customer to Edit");
		lblSelectCustomerTo.setBounds(12, 160, 142, 16);
		contentPane.add(lblSelectCustomerTo);
		
		JButton btnNewButton = new JButton("Edit Customer");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selectedCustomer = (String)cmbCustomers.getSelectedItem();
				EditCustomerScr editProductScr = new EditCustomerScr(selectedCustomer);
				editProductScr.setVisible(true);
			}
		});
		btnNewButton.setBounds(283, 156, 113, 25);
		contentPane.add(btnNewButton);
		
		table = new JTable();
		table.setBounds(283, 91, 1, 1);
		contentPane.add(table);
	}
}
